﻿(function () {
    "use strict";

    var list = new WinJS.Binding.List();
    var groupedItems = list.createGrouped(
        function groupKeySelector(item) { return item.group.key; },
        function groupDataSelector(item) { return item.group; }
    );

    // TODO: Replace the data with your real data.
    // You can add data from asynchronous sources whenever it becomes available.

    WinJS.xhr({ url: "http://services.huynhanhtuan.com/student.svc/getallsong" }).done(onComplete, onError, onprogress);

    function onComplete(xhr) {
        var items = xhr.responseXML.querySelectorAll("Song");

        for (var i = 0, len = items.length; i < len; i++) {
            var item = items[i];

            var SongName = item.querySelector("SongName").textContent;
            var GroupId = item.querySelector("Group").querySelector("GroupId").textContent;
            var GroupName = item.querySelector("Group").querySelector("Title").textContent;
            var GroupDescription = item.querySelector("Group").querySelector("Description").textContent;
            var PercentOfFavorite = item.querySelector("PercentOfFavorite").textContent;
            var SongID = item.querySelector("SongID").textContent;
            var DateCreated = item.querySelector("DateCreated").textContent;
            var Username = item.querySelector("Username").textContent;
            var Time = item.querySelector("Time").textContent;
            var Content = item.querySelector("Content").textContent;

            var group = { key: GroupId, title: GroupName, description: GroupDescription };
            var item = { group: group, SongId: SongID, SongName: SongName, Time: Time, Username: Username, Content: Content, PercentOfFavorite: PercentOfFavorite, DateCreated: DateCreated };



            list.push(item);
        }
    }

    WinJS.xhr({ url: "data.xml" }).done(onCompletelocal, onErrorlocal, onprogresslocal);

    function onCompletelocal(xhr) {
        var items = xhr.responseXML.querySelectorAll("Song");

        for (var i = 0, len = items.length; i < len; i++) {
            var item = items[i];

            var SongName = item.querySelector("SongName").textContent;
            var GroupId = item.querySelector("Group").querySelector("GroupId").textContent;
            var GroupName = item.querySelector("Group").querySelector("Title").textContent;
            var GroupDescription = item.querySelector("Group").querySelector("Description").textContent;
            var PercentOfFavorite = item.querySelector("PercentOfFavorite").textContent;
            var SongID = item.querySelector("SongID").textContent;
            var DateCreated = item.querySelector("DateCreated").textContent;
            var Username = item.querySelector("Username").textContent;
            var Time = item.querySelector("Time").textContent;
            var Content = item.querySelector("Content").textContent;

            var group = { key: GroupId, title: GroupName, description: GroupDescription };
            var item = { group: group, SongId: SongID, SongName: SongName, Time: Time, Username: Username, Content: Content, PercentOfFavorite: PercentOfFavorite, DateCreated: DateCreated };



            list.push(item);
        }
    }

    function onprogress() {
    }
    function onError() {
    }

    function onprogresslocal() {
    }
    function onErrorlocal() {
    }

    //WinJS.xhr({ url: "http://services.huynhanhtuan.com/student.svc/getallsong" }).then(function (xhr) {
    //    //var items = JSON.parse(xhr.responseText);
    //    var items = xhr.responseXML.querySelectorAll("Song");

    //    for (var i = 0, len = items.length; i < len; i++) {
    //        var item = items[i];

    //        var SongName = item.querySelector("SongName").textContent;
    //        var GroupId = item.querySelector("Group").querySelector("GroupId").textContent;
    //        var GroupName = item.querySelector("Group").querySelector("Title").textContent;
    //        var GroupDescription = item.querySelector("Group").querySelector("Description").textContent;
    //        var PercentOfFavorite = item.querySelector("PercentOfFavorite").textContent;
    //        var SongID = item.querySelector("SongID").textContent;
    //        var DateCreated = item.querySelector("DateCreated").textContent;
    //        var Username = item.querySelector("Username").textContent;
    //        var Time = item.querySelector("Time").textContent;
    //        var Content = item.querySelector("Content").textContent;

    //        var group = { key: GroupId, title: GroupName, description: GroupDescription };
    //        var item = { group: group, SongId: SongID, SongName: SongName, Time: Time, Username: Username, Content: Content, PercentOfFavorite: PercentOfFavorite, DateCreated: DateCreated };



    //        list.push(item);
    //    }

        
    //});

    WinJS.Namespace.define("Data", {
        items: groupedItems,
        groups: groupedItems.groups,
        getItemReference: getItemReference,
        getItemsFromGroup: getItemsFromGroup,
        resolveGroupReference: resolveGroupReference,
        resolveItemReference: resolveItemReference,
        getItemFromQuery: getItemFromQuery,
        loadLocalData: loadLocalData,
        saveData:saveData
    });

    function loadLocalData(filename) {
        var roamingFolder = Windows.Storage.ApplicationData.current.roamingFolder;
        var content;
        roamingFolder.getFileAsync(filename).
        then(function (file) {
            return Windows.Storage.FileIO.readTextAsync(file);
        }).done(function (text) {
            var content = text;
            //document.getElementById("filesOutput").innerText = "Counter: " + counter;
        }, function () {
            // getFileAsync or readTextAsync failed.
            //document.getElementById("filesOutput").innerText = "Counter: <not found>";
        });

    }

    function saveData(filename) {
        var roamingFolder = Windows.Storage.ApplicationData.current.roamingFolder;
        var content;
        content = generateSampleData().toString();
        roamingFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting)
            .then(function (file) {
                return Windows.Storage.FileIO.writeTextAsync(file, content);
            }).done(function () {
                
            });

    }

    // Get a reference for an item, using the group key and item title as a
    // unique reference to the item that can be easily serialized.
    function getItemReference(item) {
        return [item.group.key, item.title];
    }

    // This function returns a WinJS.Binding.List containing only the items
    // that belong to the provided group.
    function getItemsFromGroup(group) {
        return list.createFiltered(function (item) { return item.group.key === group.key; });
    }

    function getItemFromQuery(str)
    {
        return list.createFiltered(function (item) { return item.SongName === str; });
    }

    // Get the unique group corresponding to the provided group key.
    function resolveGroupReference(key) {
        for (var i = 0; i < groupedItems.groups.length; i++) {
            if (groupedItems.groups.getAt(i).key === key) {
                return groupedItems.groups.getAt(i);
            }
        }
    }

    
    // Get a unique item from the provided string array, which should contain a
    // group key and an item title.
    function resolveItemReference(reference) {
        for (var i = 0; i < groupedItems.length; i++) {
            var item = groupedItems.getAt(i);
            if (item.group.key === reference[0] && item.title === reference[1]) {
                return item;
            }
        }
    }

    // Returns an array of sample data that can be added to the application's
    // data list. 
    function generateSampleData() {
        var content = "dhfksjfhgsd";
        


        var itemContent = "Song content";
        var itemDescription = "Song description";
        var groupDescription = "Group Description";

        // These three strings encode placeholder images. You will want to set the
        // backgroundImage property in your real data to be URLs to images.
        var darkGray = "";
        var lightGray = "";
        var mediumGray = "";

        // Each of these sample groups must have a unique key to be displayed
        // separately.
        var sampleGroups = [
            { key: "grpFavorite", title: "Favorite", subtitle: "Your favorite record", backgroundImage: darkGray, description: groupDescription },
            { key: "grpSocial", title: "Social", subtitle: "Get from social", backgroundImage: lightGray, description: groupDescription },
            { key: "grpLocal", title: "Local", subtitle: "Your local data", backgroundImage: mediumGray, description: groupDescription },
        ];

        // Each of these sample items should have a reference to a particular
        // group.
        var sampleItems = [
            { group: sampleGroups[0], SongId:"1", SongName: "Sorry",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"100px",DateCreated:"1991-10-10" },
            { group: sampleGroups[0], songId: "2", SongName: "Love to be loved", Username: "tampham", content: content, PercentOfFavorite: "50px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[0], SongId: "3", SongName: "My heart will go on", Time: "3:10", Username: "hau", Content: content, PercentOfFavorite: "200px", DateCreated: "1991-10-10" },
            { group: sampleGroups[0], songId: "4", SongName: "Love to be loved", Username: "Dong Nguyen", content: content, PercentOfFavorite: "10px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[0], SongId:"5", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"150px",DateCreated:"1991-10-10" },
            { group: sampleGroups[0], songId: "6", SongName: "Love to be loved", Username: "tuanhuynh", content: content, PercentOfFavorite: "200px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[0], SongId:"7", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"250px",DateCreated:"1991-10-10" },
            { group: sampleGroups[0], songId: "8", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "250px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[0], SongId:"9", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[0], songId: "10", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "130px", DateCreated: "1991-10-10" },


			{ group: sampleGroups[1], SongId:"11", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[1], songId: "12", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[1], SongId:"13", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[1], songId: "14", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[1], SongId:"15", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[1], songId: "16", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[1], SongId:"17", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[1], songId: "18", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[1], SongId:"19", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[1], songId: "20", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },

			{ group: sampleGroups[2], SongId:"111", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[2], songId: "112", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[2], SongId:"113", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[2], songId: "114", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[2], SongId:"115", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[2], songId: "116", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[2], SongId:"117", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[2], songId: "118", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },
			{ group: sampleGroups[2], SongId:"119", SongName: "My heart will go on",Time:"3:10", Username: "tuanhuynh", Content: content, PercentOfFavorite:"200px",DateCreated:"1991-10-10" },
            { group: sampleGroups[2], songId: "210", title: "Love to be loved", author: "tuanhuynh", content: content, percentageOfFavorite: "90px", DateCreated: "1991-10-10" },

        ];

        
        return sampleItems;
    }

    
})();
